package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.corona.Game;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;

/* compiled from: Network.java */
/* loaded from: pokewilds.jar:com/pkmngen/game/ClientBroadcast.class */
public class ClientBroadcast extends Action {
    int timeStep = 0;
    public Action.Layer layer = Action.Layer.map_1;
    int playerIndex = 1;

    public Action deserializeAction(Network.ActionData actionData) {
        Action newAction = null;
        try {
            Class cl = Class.forName(actionData.className);
            Class[] paramTypes = new Class[actionData.params.length];
            for (int i = 0; i < actionData.params.length; i++) {
                if (actionData.params[i] instanceof Network.ActionData) {
                    actionData.params[i] = deserializeAction((Network.ActionData) actionData.params[i]);
                }
                paramTypes[i] = actionData.params[i].getClass();
            }
            newAction = (Action) cl.getConstructor(paramTypes).newInstance(actionData.params);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e2) {
            e2.printStackTrace();
        } catch (IllegalArgumentException e3) {
            e3.printStackTrace();
        } catch (InstantiationException e4) {
            e4.printStackTrace();
        } catch (NoSuchMethodException e5) {
            e5.printStackTrace();
        } catch (SecurityException e6) {
            e6.printStackTrace();
        } catch (InvocationTargetException e7) {
            e7.printStackTrace();
        }
        return newAction;
    }

    public ClientBroadcast(final Game game) {
        super(new Object[0]);
        game.client.addListener(new Listener() { // from class: com.corona.game.ClientBroadcast.1
            @Override // com.esotericsoftware.kryonet.Listener
            public void connected(Connection connection) {
            }

            @Override // com.esotericsoftware.kryonet.Listener
            public void received(Connection connection, final Object object) {
                if (object instanceof Network.ActionData) {
                    game.insertAction(ClientBroadcast.this.deserializeAction((Network.ActionData) object));
                }
                Runnable runnable = new Runnable() { // from class: com.corona.game.ClientBroadcast.1.1
                    @Override // java.lang.Runnable
                    public void run() {
                        try {
                            if (object instanceof Network.PlayerData) {
                                game.player = new Player((Network.PlayerData) object);
                                game.cam.position.set(game.player.position.x + 16.0f, game.player.position.y, 0.0f);
                            } else if (object instanceof Network.RelocatePlayer) {
                                Network.RelocatePlayer relocatePlayer = (Network.RelocatePlayer) object;
                                game.player.position = relocatePlayer.position.cpy();
                                game.cam.position.set(relocatePlayer.position.cpy().add(16.0f, 0.0f), 0.0f);
                            } else if (object instanceof Network.Logout) {
                                Network.Logout logout = (Network.Logout) object;
                                if (!game.players.containsKey(logout.playerId)) {
                                    System.out.println("Logout: Invalid player ID " + logout.playerId + ", sent by server");
                                    throw new Exception();
                                }
                                game.actionStack.remove(game.players.get(logout.playerId).standingAction);
                                game.players.remove(logout.playerId);
                            } else if (object instanceof Network.MapTiles) {
                                Network.MapTiles mapTiles = (Network.MapTiles) object;
                                Iterator<Network.TileData> it = mapTiles.tiles.iterator();
                                while (it.hasNext()) {
                                    Network.TileDataBase tileData = it.next();
                                    Tile tile = Tile.get(tileData, null);
                                    if (tileData.interiorIndex != 0) {
                                        game.map.interiorTiles.get(tileData.interiorIndex).put(tileData.pos.cpy(), tile);
                                    } else {
                                        game.map.overworldTiles.put(tileData.pos.cpy(), tile);
                                    }
                                }
                                if (mapTiles.timeOfDay != null) {
                                    CycleDayNight.dayTimer = mapTiles.dayTimer;
                                    game.map.timeOfDay = mapTiles.timeOfDay;
                                }
                            } else if (object instanceof Network.ServerPlayerData) {
                                Network.ServerPlayerData serverPlayerData = (Network.ServerPlayerData) object;
                                if (!game.players.containsKey(serverPlayerData.number)) {
                                    Player player = new Player();
                                    player.name = serverPlayerData.name;
                                    player.position = serverPlayerData.position.cpy();
                                    player.type = Player.Type.REMOTE;
                                    player.network.id = serverPlayerData.number;
                                    if (serverPlayerData.isInterior) {
                                        player.network.tiles = game.map.interiorTiles.get(game.map.interiorTilesIndex);
                                    } else {
                                        player.network.tiles = game.map.overworldTiles;
                                    }
                                    player.setColor(serverPlayerData.color);
                                    game.players.put(serverPlayerData.number, player);
                                }
                                Player player2 = game.players.get(serverPlayerData.number);
                                if (game.actionStack.contains(player2.standingAction)) {
                                    player2.standingAction = new PlayerStanding(game, player2, false, true);
                                    game.insertAction(player2.standingAction);
                                }
                            } else if (object instanceof Network.MovePlayer) {
                                Network.MovePlayer movePlayer = (Network.MovePlayer) object;
                                if (!game.players.containsKey(movePlayer.playerId)) {
                                    System.out.println("MovePlayer: Invalid player ID " + movePlayer.playerId + ", sent by server");
                                    throw new Exception();
                                }
                                Player player3 = game.players.get(movePlayer.playerId);
                                player3.network.shouldMove = true;
                                player3.network.dirFacing = movePlayer.dirFacing;
                                player3.network.isRunning = movePlayer.isRunning;
                            } else if (object instanceof Network.MovePokemon) {
                                Network.MovePokemon movePokemon = (Network.MovePokemon) object;
                                if (!game.map.pokemon.containsKey(movePokemon.position)) {
                                    System.out.println("MovePokemon: Invalid pokemon position " + movePokemon.position.toString() + ", sent by server");
                                    throw new Exception();
                                }
                                Pokemon pokemon = game.map.pokemon.get(movePokemon.position);
                                pokemon.dirFacing = movePokemon.dirFacing;
                                pokemon.shouldMove = true;
                            } else if (object instanceof Network.BattleData) {
                                game.player.network.doEncounter = (Network.BattleData) object;
                            } else if (object instanceof Network.BattleTurnData) {
                                game.battle.network.turnData = (Network.BattleTurnData) object;
                            } else if (object instanceof Network.TileData) {
                                Network.TileData tileData2 = (Network.TileData) object;
                                Tile newTile = new Tile(tileData2.tileName, tileData2.tileNameUpper, tileData2.pos.cpy(), true, null);
                                newTile.hasItem = tileData2.hasItem;
                                newTile.hasItemAmount = tileData2.hasItemAmount;
                                game.map.tiles.put(tileData2.pos.cpy(), newTile);
                                game.map.adjustSurroundingTiles(newTile);
                            } else if (object instanceof Network.OverworldPokemonData) {
                                Network.OverworldPokemonData pokemonData = (Network.OverworldPokemonData) object;
                                if (pokemonData.remove) {
                                    Pokemon pokemon2 = game.map.pokemon.get(pokemonData.overworldPos);
                                    if (pokemon2 == null) {
                                        System.out.println("OverworldPokemonData: Invalid position " + pokemonData.overworldPos.toString() + ", sent by server");
                                        throw new Exception();
                                    } else {
                                        game.map.pokemon.remove(pokemonData.overworldPos);
                                        game.actionStack.remove(pokemon2.standingAction);
                                    }
                                } else {
                                    Pokemon pokemon3 = new Pokemon(pokemonData);
                                    pokemon3.position = pokemonData.overworldPos.cpy();
                                    game.map.pokemon.put(pokemonData.overworldPos, pokemon3);
                                    pokemon3.type = Player.Type.REMOTE;
                                    Game game2 = game;
                                    Objects.requireNonNull(pokemon3);
                                    game2.insertAction(new Pokemon.Standing());
                                }
                            } else if (object instanceof Network.PokemonData) {
                                Network.PokemonData pokemonData2 = (Network.PokemonData) object;
                                game.player.pokemon.set(pokemonData2.index, new Pokemon(pokemonData2));
                            } else if (object instanceof Network.PickupItem) {
                                Tile tile2 = game.map.tiles.get(((Network.PickupItem) object).pos);
                                game.playerCanMove = false;
                                String number = "a";
                                String plural = "";
                                if (tile2.hasItemAmount > 1) {
                                    number = String.valueOf(tile2.hasItemAmount);
                                    plural = "S";
                                    if (tile2.hasItem.endsWith("s")) {
                                        plural = "ES";
                                    }
                                }
                                game.insertAction(new DisplayText(game, "Found " + number + " " + tile2.hasItem.toUpperCase(Locale.ROOT) + plural + "!", "fanfare1.ogg", (Action) null, new SetField(game, "playerCanMove", true, null)));
                                tile2.pickUpItem(game.player);
                            } else if (object instanceof Network.UseHM) {
                                Network.UseHM useHM = (Network.UseHM) object;
                                if (!game.players.containsKey(useHM.playerId)) {
                                    System.out.println("UseHM: Invalid player id " + useHM.playerId + ", sent by: server");
                                    throw new Exception();
                                }
                                Player player4 = game.players.get(useHM.playerId);
                                if (useHM.hm.equals("STOP")) {
                                    player4.currFieldMove = "";
                                } else if (useHM.hm.equals("CUT")) {
                                    Vector2 pos = player4.facingPos(useHM.dirFacing);
                                    Tile currTile = player4.network.tiles.get(pos);
                                    if (currTile.isCuttable) {
                                        Action action = new CutTreeAnim(game, game.map.overworldTiles.get(pos), null);
                                        game.map.interiorTiles.get(game.map.interiorTilesIndex).remove(currTile.position.cpy());
                                        game.insertAction(action);
                                    }
                                } else if (useHM.hm.equals("JUMP")) {
                                    player4.currFieldMove = "RIDE";
                                }
                            } else if (object instanceof Network.UseItem) {
                                Network.UseItem useItem = (Network.UseItem) object;
                                if (!game.players.containsKey(useItem.playerId)) {
                                    System.out.println("UseHM: Invalid player id " + useItem.playerId + ", sent by: server");
                                    throw new Exception();
                                }
                                Vector2 pos2 = game.players.get(useItem.playerId).facingPos(useItem.dirFacing);
                                if (useItem.item.contains("apricorn")) {
                                    game.insertAction(new PlantTree(pos2, null));
                                }
                            } else if (object instanceof Network.PausePokemon) {
                                Network.PausePokemon pausePokemon = (Network.PausePokemon) object;
                                Pokemon pokemon4 = game.map.pokemon.get(pausePokemon.position);
                                pokemon4.canMove = false;
                                game.playerCanMove = false;
                                String oppDir = "down";
                                if (game.player.dirFacing.equals("up")) {
                                    oppDir = "down";
                                } else if (game.player.dirFacing.equals("down")) {
                                    oppDir = "up";
                                } else if (game.player.dirFacing.equals("right")) {
                                    oppDir = "left";
                                } else if (game.player.dirFacing.equals("left")) {
                                    oppDir = "right";
                                }
                                Game game3 = game;
                                Objects.requireNonNull(pokemon4);
                                Action nextAction = new SetField(pokemon4, "dirFacing", oppDir, new WaitFrames(game3, 20, new SplitAction(new Pokemon.Emote("happy", null), new WaitFrames(game, 20, new PlayMusic(pokemon4, (Action) null)))));
                                if (pokemon4.previousOwner != game.player) {
                                    nextAction.append(new DisplayText(game, pokemon4.nickname.toUpperCase(Locale.ROOT) + " seems friendly. ", (String) null, false, true, (Action) null));
                                }
                                Game game4 = game;
                                Game game5 = game;
                                Objects.requireNonNull(pokemon4);
                                DisplayText.Clear clear = new DisplayText.Clear(game4, new WaitFrames(game5, 3, new Pokemon.AddToInventory(pausePokemon.position, new SetField(game, "playerCanMove", true, new SetField(pokemon4, "canMove", true, null)))));
                                Game game6 = game;
                                Game game7 = game;
                                Game game8 = game;
                                Objects.requireNonNull(pokemon4);
                                nextAction.append(new DisplayText(game, "Add " + pokemon4.nickname.toUpperCase(Locale.ROOT) + " to your party?", (String) null, true, false, (Action) new DrawYesNoMenu(null, clear, new DisplayText.Clear(game6, new WaitFrames(game7, 3, new SetField(game8, "playerCanMove", true, new SetField(pokemon4, "canMove", true, new Pokemon.UnPause())))))));
                                game.insertAction(nextAction);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        synchronized (this) {
                            notify();
                        }
                    }
                };
                Gdx.app.postRunnable(runnable);
                try {
                    synchronized (runnable) {
                        runnable.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override // com.corona.game.Action
    public Action.Layer getLayer() {
        return this.layer;
    }
}
